<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="youxi" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="getcurId(data)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 属性的组件el-tree搭建属性的结构的数据 -->
        <!-- el-tree
          default-expand-all 是否展开所有的子节点
        -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <!-- 使用作用域插槽获取组件内部的数据
           el-tree提供的默认的作用域插槽 #default="{data}" 获取el-tree组件内部的数据
          -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="youxi" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="getcurId(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
      <el-dialog
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
        :close-on-click-modal="true"
        :close-on-press-escape="false"
        :title="isEdit ? '编辑部门' : '新增子部门'"
        @closed="hclose"
      >
        <add0rEdit
          :id="curId"
          ref="deptRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="dialogVisible=false"
        />
      </el-dialog>
    </div>

  </div>
</template>
<script>
import add0rEdit from './deptDialog.vue'
import { deleDartments, getDartments } from '@/api/departments'
import { treeInfo } from '@/utils/treeinfo'
export default {
  name: 'Departments',
  components: {
    add0rEdit
  },

  data() {
    return {
      list: [],
      dialogVisible: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  mounted() {

  },
  created() {
    this.loadDartments()
  },

  methods: {
    // 关闭弹层
    hclose() {
      this.$refs.deptRef.resetForms()
    },
    hSuccess() {
      this.loadDartments()
      this.dialogVisible = false
    },
    handleNodeClick() {
    },
    async loadDartments() {
      try {
        const res = await getDartments()
        if (res.code !== 10000) {
          return this.$message.error(res.message)
        } else {
          this.originList = res.data.depts.map(item => {
            const { id, code, name, pid } = item
            return { id, code, name, pid }
          })
        }
        res.data.depts.shift()
        this.list = treeInfo(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },

    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    getcurId(data) {
      this.dialogVisible = true
      this.curId = data.id
      this.isEdit = false
    },
    hEdit(data) {
      this.dialogVisible = true
      this.curId = data.id
      this.isEdit = true
    },
    hDel(data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(data.id)
        const res = await deleDartments(data.id)
        if (res.code === 10000) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadDartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }

}
</script>

<style lang="sass" scoped>

</style>
